﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tool;
using AutoMachine;

namespace CommonTool
{
   public class LogView : SingletonTemplate<LogView>
    {
        /// <summary>
        /// 通讯文件锁，防止文件冲突
        /// </summary>
        private static object objOper = new object();
        /// <summary>
        /// 异常文件锁，防止文件冲突
        /// </summary>
        private static object objError = new object();

        private RichTextBox m_RichTextBox = null;

        public enum LogType
        {
            Oper,//操作文件
            Error,//错误文件
        }
        /// <summary>
       /// 日志信息显示事件
       /// </summary>
        public event LogView.LogHandler LogEvent;

        /// <summary>
       /// 触发日志信息显示事件
       /// </summary>
       /// <param name="strLog"></param>
        public void ShowLog( LogType logType ,string strLog)
        {       
            if (this.LogEvent == null || this.m_RichTextBox == null)
                return;
            ShowLogSave( logType,strLog);
            this.LogEvent(this.m_RichTextBox, logType, strLog);
        }

        /// <summary>
     /// 设置日志显示在哪个列表框上
     /// </summary>
     /// <param name="logListBox"></param>
        public void SetLogRichTextBox(RichTextBox logRichTextBox)
        {
            this.m_RichTextBox = logRichTextBox;
         }

       /// <summary>
       /// 日志信息显示委托函数
       /// </summary>
       /// <param name="logListBox"></param>
       /// <param name="strLog"></param>
        public delegate void LogHandler(RichTextBox logRichTextBox, LogType logType, string text);

        /// <summary>
        /// 保存日志到文件中
        /// </summary>
        /// <param name="strLog"></param>
        public void ShowLogSave( LogType logType, string text )
        {

            string Path = SysConfig.Config["日志存储路径"].ToString();

            if (!Directory.Exists(Path))//判断文件夹是否存在
            {
                //创建文件夹
                Directory.CreateDirectory(Path);
            }
            try
            {
                switch (logType)
                {
                    case LogType.Oper:
                        lock(objOper)
                        {
                            string logPath = Path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + "操作日志" + ".txt";
                            using (StreamWriter sw = File.AppendText(logPath))
                            {
                                sw.WriteLine("时间：" + DateTime.Now.ToString(" HH:mm:ss") + " " + text);//写入内容                                                                                                
                                sw.Flush();//清理当前写入器的所有缓冲区
                                sw.Close();//关闭流
                                sw.Dispose();//释放资源
                            }
                        }
                        break;
                    case LogType.Error:
                        lock (objError)
                        {
                            string logPath = Path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + "错误日志" + ".txt";
                            //委托报警代码
                            ShowWarn(text);                       
                            using (StreamWriter sw = File.AppendText(logPath))
                            {
                                sw.WriteLine("时间：" + DateTime.Now.ToString(" HH:mm:ss") + " " + text);//写入内容                                                                                                
                                sw.Flush();//清理当前写入器的所有缓冲区
                                sw.Close();//关闭流
                                sw.Dispose();//释放资源
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }



        //报警代码柔和到Log显示里面

        private DataGridView m_DataGridView = null;


        //报警事件
        public event WarnHandler WarnEvent;

        //报警委托函数
        public delegate void WarnHandler(string text);

        public void ShowWarn(string text)
        {
            if (this.WarnEvent == null || this.m_DataGridView == null)
                return;
            this.WarnEvent(text);       
        }

        public void SetWarnDataGridView(DataGridView logDataGridView)
        {
            this.m_DataGridView = logDataGridView;
        }
    }
}
